const Actions = require('./actions');
const DB = require('app/db.js');
import { Storage } from 'clientConfig/util/StoreData'
import { Toast } from 'saltui';
import { ready } from 'clientConfig/util/queryurlfield'
const device = require('clientConfig/util/jsapi/device');
module.exports = Reflux.createStore({
    listenables: [Actions],
    data: {
        title: ''
    },
    onFetchSanitationList: function (pageNum, props, success, error) {
        let t = this;
        // ready(() => { device.showPreloader() })
        DB.Teacher.get_sanitation_list({
            schoolId: Storage.get('user.schoolId'),
            pageIndex: pageNum,
            classId: props.classId
        })
            .then((content) => {
                // ready(() => { device.hidePreloader() })
                if (content.code == 1) {
                    let schoolId = content.check.schoolId;
                    var list = [];
                    var obj = {};
                    if (content && content.data) {
                        content = content.data.list;
                    }
                    if (content && content.length > 0) {
                        for (let item of content) {
                            obj["schoolId"] = schoolId;
                            obj["title"] = item.title;
                            obj["sanitationId"] = item.sanitationId;
                            obj["checkDate"] = item.checkDate;
                            obj["scoreList"] = item.scoreList;
                            list.push(obj);
                            obj = {};
                        }
                    }
                    t.data.classId = props.classId;
                    for (let item of t.data.listdata) {
                        if (item.classId == props.classId) {
                            t.data.title = item.title;
                        }
                    }
                    const hasNoMore = list.length < props.pageSize;
                    t.data.listData = list;
                    if (success) {
                        success(hasNoMore, list);
                    } else if (!content || !success) {
                        error();
                    }
                }else{
                    Toast.show({
                        type: 'error',
                        content: '系统异常'
                    });
                }
            })
            .catch(function (error) {
                Toast.show({
                    type: 'error',
                    content: '系统异常'
                });
            });
    },
    onGetClassList: function (params, cb) {
        let t = this;
        DB.Teacher.get_class_list({
            schoolId: Storage.get('user.schoolId'),
            teacherId: Storage.get('user.teacherId')
        })
            .then((content) => {

                if (content.code == 1) {
                    let schoolId = content.check.schoolId;
                    let teacherId = content.check.teacherId;
                    content = content.data.list;
                    var list = [];
                    var obj = {};
                    if (content && content.length > 0) {
                        for (let item of content) {
                            obj.imgUrl = '';
                            obj.classId = item.id;
                            obj.title = item.name;
                            list.push(obj);
                            obj = {};
                        }
                        t.data.listdata = list;
                        if (!t.data.classId) {
                            t.data.classId = list[0].classId;
                            t.data.title = list[0].title;
                        }
                        t.updateComponent();
                    }
                }else{
                    Toast.show({
                        type: 'error',
                        content: '系统异常'
                    });
                }
            })
            .catch(function (error) {
                Toast.show({
                    type: 'error',
                    content: '系统异常'
                });
            });
    },
    onPermissionJudgment: function (params, cb) {
        let t = this;
        DB.Teacher.judge_tch_IsMarker({
            schoolId: Storage.get('user.schoolId'),
            teacherId: Storage.get('user.teacherId')
        })
            .then((content) => {

                if (content.code == 1) {
                    t.data.isMarker = content.data.isMarker;
                    t.updateComponent();
                }else{
                    Toast.show({
                        type: 'error',
                        content: '系统异常'
                    });
                }
            })
            .catch(function (error) {
                Toast.show({
                    type: 'error',
                    content: '系统异常'
                });
            });
    },
    updateComponent: function () {
        this.trigger(this.data);
    }
});
